package com.cnhis.drgs.util;

import org.apache.commons.lang.StringUtils;

import java.util.Calendar;
import java.util.Date;

public class DateTimeForm {

    //时间类型(1本年 2本月 3本日 4手动选择)
    private Integer dateTimeType;

    //开始日期
    private String beginDateStr;

    //结束日期
    private String endDateStr;

    //开始时间
    private String beginDateTimeStr;

    //结束时间
    private String endDateTimeStr;


    public Date getBeginDate() {
        Date date = null;
        if (StringUtils.isNotBlank(beginDateTimeStr)) {
            date = DateUtil.parseDate(beginDateTimeStr, "yyyy-MM-dd HH:mm:ss");
        } else if (StringUtils.isNotBlank(beginDateStr)) {
            date = DateUtil.parseDate(beginDateStr + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        } else if (dateTimeType != null) {
            Calendar calendar;
            switch (dateTimeType) {
                case 1:
                    calendar = Calendar.getInstance();
                    calendar.set(Calendar.MONTH, 0);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    date = calendar.getTime();
                    break;
                case 2:
                    calendar = Calendar.getInstance();
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    date = calendar.getTime();
                    break;
                case 3:
                    calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    date = calendar.getTime();
                    break;
            }
        }

        if (date == null) {
            date = DateUtil.parseDate(DateUtil.formatDate(new Date(),"yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
        }

        return date;
    }

    public Date getEndDate() {
        Date date = null;
        if (StringUtils.isNotBlank(endDateTimeStr)) {
            date = DateUtil.parseDate(endDateTimeStr, "yyyy-MM-dd HH:mm:ss");
        } else if (StringUtils.isNotBlank(endDateStr)) {
            date = DateUtil.parseDate(endDateStr + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
        } else if (dateTimeType != null) {
            Calendar calendar;
            switch (dateTimeType) {
                case 1:
                    calendar = Calendar.getInstance();
                    calendar.set(Calendar.MONTH, 11);
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    calendar.set(Calendar.MILLISECOND, 999);
                    date = calendar.getTime();
                    break;
                case 2:
                    calendar = Calendar.getInstance();
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    calendar.set(Calendar.MILLISECOND, 999);
                    date = calendar.getTime();
                    break;
                case 3:
                    calendar = Calendar.getInstance();
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    calendar.set(Calendar.MILLISECOND, 999);
                    date = calendar.getTime();
                    break;
            }
        }
        if (date != null && date.compareTo(new Date()) > 0) {
            date = new Date();
        }

        if (date == null) {
            date = new Date();
        }

        return date;
    }

    public Integer getDateTimeType() {
        if (StringUtils.isNotBlank(beginDateStr)
                || StringUtils.isNotBlank(beginDateTimeStr)
                || StringUtils.isNotBlank(endDateStr)
                || StringUtils.isNotBlank(endDateTimeStr)
                ) {
            dateTimeType = 4;
        }
        return dateTimeType;
    }

    public void setDateTimeType(Integer dateTimeType) {
        this.dateTimeType = dateTimeType;
    }

    public String getBeginDateStr() {
        return beginDateStr;
    }

    public void setBeginDateStr(String beginDateStr) {
        this.beginDateStr = beginDateStr;
    }

    public String getEndDateStr() {
        return endDateStr;
    }

    public void setEndDateStr(String endDateStr) {
        this.endDateStr = endDateStr;
    }

    public String getBeginDateTimeStr() {
        return beginDateTimeStr;
    }

    public void setBeginDateTimeStr(String beginDateTimeStr) {
        this.beginDateTimeStr = beginDateTimeStr;
    }

    public String getEndDateTimeStr() {
        return endDateTimeStr;
    }

    public void setEndDateTimeStr(String endDateTimeStr) {
        this.endDateTimeStr = endDateTimeStr;
    }


}
